<?php
/**
 * Created by PhpStorm.
 * User: 小灰灰
 * Date: 2022-06-15
 * Time: 10:45:22
 * Info: 根据时间构造crontab任务计划
 */

namespace support\lib;

class Crontab
{

    public function GetCrondCycle($param)
    {
        $cuonConfig = "";
        switch ($param['time_cycle']) {
            case 'day':
                $cuonConfig = $this->GetDay($param);
                break;
            case 'day-n':
                $cuonConfig = $this->GetDay_N($param);
                break;
            case 'hour':
                $cuonConfig = $this->GetHour($param);
                break;
            case 'hour-n':
                $cuonConfig = $this->GetHour_N($param);
                break;
            case 'minute-n':
                $cuonConfig = $this->Minute_N($param);
                break;
            case 'second-n':
                $cuonConfig = $this->Second_N($param);
                break;
            case 'week':
                $cuonConfig = $this->Week($param);
                break;
            case 'month':
                $cuonConfig = $this->Month($param);
                break;
        }

        return $cuonConfig;
    }

    //取任务构造Day
    private function GetDay($param)
    {
        $cuonConfig = "{$param['minute']} {$param['hour']} * * * ";

        return $cuonConfig;
    }

    //取任务构造Day_N
    private function GetDay_N($param)
    {
        $cuonConfig = "{$param['minute']} {$param['hour']} */{$param['where1']} * * ";

        return $cuonConfig;
    }

    //取任务构造Hour
    private function GetHour($param)
    {
        $cuonConfig = "{$param['minute']} * * * * ";

        return $cuonConfig;
    }

    //取任务构造Hour-N
    private function GetHour_N($param)
    {
        $cuonConfig = "{$param['minute']} */{$param['hour']} * * * ";

        return $cuonConfig;
    }

    //取任务构造Minute-N
    private function Minute_N($param)
    {
        $cuonConfig = "*/{$param['minute']} * * * * ";

        return $cuonConfig;
    }

    //取任务构造Second-N
    private function Second_N($param)
    {
        $cuonConfig = "*/{$param['second']} * * * * * ";

        return $cuonConfig;
    }

    //取任务构造week
    private function Week($param)
    {
        $cuonConfig = "{$param['minute']} {$param['hour']} * * {$param['week']}";

        return $cuonConfig;
    }

    //取任务构造Month
    private function Month($param)
    {
        $cuonConfig = "{$param['minute']} {$param['hour']} {$param['where1']} * * ";

        return $cuonConfig;
    }

}
